﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AlexPilotti.FTPS.Client;
using AlexPilotti.FTPS.Common;
using System.Net;
using System.IO;


namespace YLServerInterface.FTP
{
    public class FTP
    {
        private string host;
        private NetworkCredential credential;
        private FTPSClient client;
        public FTP(int[] ip, string name, string pwd)
        {
            this.host = getHost(ip);
            credential = new NetworkCredential(name, pwd);

        }
        private string getHost(int[] ip)
        {
            return host = ip.Select(i => i.ToString()).Aggregate((acc, str) => acc += "." + str);
        }
        public FTP(string host, string name, string pwd)
        {
            this.host = host;
            credential = new NetworkCredential(name, pwd);
        }
        public void DownloadFile(string remoteFileName, string outputFileName)
        {
            client = new FTPSClient();
            client.Connect(host, credential, ESSLSupportMode.ClearText);
            var fs = client.GetFile(remoteFileName);
            saveFile(fs, outputFileName);
            fs.Close();
            client.Close();
        }
        /// <summary>
        /// 单独发送文件，发送完毕断开连接，不适合多文件发送。
        /// </summary>
        /// <param name="host"></param>
        /// <param name="credential"></param>
        /// <param name="remoteFileName"></param>
        /// <param name="localFileName"></param>
        public void UploadFile(string host, NetworkCredential credential, string remoteFileName, string localFileName)
        {
            client.Connect(host, credential, ESSLSupportMode.ClearText);
            client.PutFile(localFileName, remoteFileName);
            client.Close();
        }
        /// <summary>
        /// 使用前需要调用connect，使用后需要关闭连接。
        /// </summary>
        /// <param name="remoteFileName"></param>
        /// <param name="localFileName"></param>
        public void UploadFile(string remoteFileName, string localFileName)
        {
            client = new FTPSClient();
            client.Connect(host, credential, ESSLSupportMode.ClearText);
            client.PutFile(localFileName, remoteFileName);
            client.Close();
        }
        public void Connect()
        {
            if (client == null)
            {
                client = new FTPSClient();
                client.Connect(host, credential, ESSLSupportMode.ClearText);
            }
        }
        public void Close()
        {
            client.Close();
            client = null;
        }
        public void UploadFiles(string dirName)
        {
            client.Connect(host, credential, ESSLSupportMode.ClearText);
            client.PutFiles(dirName);
            client.Close();
        }
        /// <summary>
        /// localfilename,remoteFileName
        /// </summary>
        /// <param name="files"></param>
        public void UploadFiles(IEnumerable<Tuple<string, string>> files)
        {
            client = new FTPSClient();
            client.Connect(host, credential, ESSLSupportMode.ClearText);
            foreach (var file in files)
            {
                client.PutFile(file.Item1, file.Item2);
            }
            client.Close();
        }
        public void DownloadFiles(string remoteDir, string localDir)
        {
            client = new FTPSClient();
            client.Connect(host, credential, ESSLSupportMode.ClearText);
            client.SetCurrentDirectory(remoteDir);
            client.GetFiles(localDir);
            client.Close();
        }
        public bool TyUploadFile(string host, NetworkCredential credential, string rFn, string lFn)
        {
            try
            {
                UploadFile(host, credential, rFn, lFn);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        private void saveFile(FTPStream input, string outputFileName)
        {
            FileStream fs;
            if (File.Exists(outputFileName) == false)
            {
                fs = File.Create(outputFileName);

            }
            else
            {
                fs = File.OpenWrite(outputFileName);
            }
            byte[] buff = new byte[1024];
            int num = input.Read(buff, 0, buff.Length);
            while (num != 0)
            {
                fs.Write(buff, 0, num);
                num = input.Read(buff, 0, buff.Length);
            }
            input.Close();
            fs.Close();
        }
    }
}
